package com.lib.index;

import java.io.Serializable;

import com.lib.model.encryptedIndex;



import Jama.Matrix;

public class indexEncrypted implements Serializable{
	private static final long serialVersionUID = 9L;
	private  int []S;
	private  Matrix M1;
	private  Matrix M2;//秘钥三元组用于加密索引向量

    public int[] getS() {
		return S;
	}

	public void setS(int[] s) {
		S = s;
	}

	public Matrix getM1() {
		return M1;
	}

	public void setM1(Matrix m1) {
		M1 = m1;
	}

	public Matrix getM2() {
		return M2;
	}

	public void setM2(Matrix m2) {
		M2 = m2;
	}
    public indexEncrypted()
    {
    	//int n=(int)(indexHelper.wordDisCount*1.25);//加密索引的长度
    	int n=indexHelper.wordDisCount+40;//40个dummuy keyword位置
		S=indexBuildTool.getRandomArray(n);
		M1=Matrix.random(n, n);
		M2=Matrix.random(n, n);
    }
	/**
	 * 对明文索引plainIndex按照KNN进行加密
	 * @param plainIndex
	 * @return
	 */
	public  encryptedIndex IndexEncrypted(double[] plainIndex)
	{
		encryptedIndex resultEncryptedIndex=new encryptedIndex();
		double []tempMD1=new double[plainIndex.length];
		double []tempMD2=new double[plainIndex.length];
		for(int i=0;i<S.length;i++)
		{
			//如果S[i]==0,那么tempMD1[i]=tempMD2[i]=plainIndex[i]
			if(S[i]==0)
			{
				tempMD1[i]=tempMD2[i]=plainIndex[i];
			}
			//如果S[i]==1,那么tempMD1[i]+tempMD2[i]=plainIndex[i]
			else {
				double []sp=indexBuildTool.splitDouble(plainIndex[i]);
				tempMD1[i]=sp[0];
				tempMD2[i]=sp[1];
			}
		}
		Matrix encryMatrix1=M1.transpose().times(new Matrix(tempMD1,1).transpose());
		Matrix encryMatrix2=M2.transpose().times(new Matrix(tempMD2,1).transpose());
		tempMD1=indexBuildTool.getArrayFromMatrix(encryMatrix1);
		tempMD2=indexBuildTool.getArrayFromMatrix(encryMatrix2);
		resultEncryptedIndex.setMD1(tempMD1);
		resultEncryptedIndex.setMD2(tempMD2);
		return resultEncryptedIndex;
	}
	
}
